
package com.ebrg.secure9.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.ebrg.secure9.Account;
import com.ebrg.secure9.Preferences;
import com.ebrg.secure9.activity.setup.EncryptionManageKey;
import com.ebrg.secure9.activity.setup.EncryptionNewKey;
import com.ebrg.secure9.mail.security.Keys;
import com.ebrg.secure9.R;



public class ChooseKey extends K9ListActivity
{
	Account mAccount;
	String mUID;
	ArrayAdapter<String> adapter;
	private ArrayList<String> accountKeys = new ArrayList<String>();
	public static final String EXTRA_ACCOUNT = "com.ebrg.secure9.ViewKeys_account";
	public static final String EXTRA_IDENTITY = "com.ebrg.secure9.ViewKeys_identity";

	protected List<String> keys = null;

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.choose_key_option, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.new_key:
			EncryptionNewKey.actionNewKey(this,mAccount,"settings", "");
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		Keys.refreshInitialKeys(this);
		Keys.refreshInitialKeys(this);
		
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		getListView().setTextFilterEnabled(true);
		getListView().setItemsCanFocus(false);
		getListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
		Intent intent = getIntent();
		String accountUuid = intent.getStringExtra(EXTRA_ACCOUNT);
		mAccount = Preferences.getPreferences(this).getAccount(accountUuid);

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

		setListAdapter(adapter);
		refreshView();
		setupClickListeners();
		
		
	}


	@Override
	public void onResume()
	{
		super.onResume();

		refreshView();

	}


	protected void refreshView() 
	{  	
		Keys.refreshKeys(this);
		Keys.refreshInitialKeys(this);	

		adapter.clear();

		//Find and add mAccount keys to list
		for(int i=0;i<Keys.keyList.size();i++)
		{
			if(Keys.keyList.get(i).startsWith(mAccount.getEmail()))
			{
				String description = cleanKeyNameForDisplay(Keys.keyList.get(i));
				adapter.add(description);
				accountKeys.add(description);
			}
		}

		//Find and add mAccount initial keys to list
		for(int i=0; i < Keys.initialKeys.size(); i++)
		{
			if(Keys.initialKeys.get(i).startsWith(mAccount.getEmail()))
			{
				String description = cleanKeyNameForDisplay(Keys.initialKeys.get(i));
				adapter.add(description);
				accountKeys.add(description);
			}
		}
	}

	private String cleanKeyNameForDisplay(String keyname)
	{
		String str = keyname.toLowerCase();

		str = str.replaceAll(mAccount.getEmail(), "");

		if(str.equals("") || str.equals(this.getString(R.string.initial_key_file_type)))
			str = str + mAccount.getEmail();

		if(str.contains(this.getString(R.string.complete_key_file_type)))	
			str = str.replace(this.getString(R.string.complete_key_file_type), "");
		else if(str.contains(this.getString(R.string.initial_key_file_type)))
			str = str.replace(this.getString(R.string.initial_key_file_type), "(Incomplete) ");			

		if(str.equals(""))
			str = str + mAccount.getEmail();

		return str;
	}

	protected void setupClickListeners()
	{
		this.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
		{
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{

				String chosen = accountKeys.get(position);
				EncryptionManageKey.actionManageKey(ChooseKey.this, mAccount, chosen);
				finish();
			}
		});

	}
}
